package com.chcit.mobile.mvp.model;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.mvp.model.api.cache.CommonCache;
import com.chcit.mobile.mvp.model.api.service.CommonService;
import com.chcit.mobile.mvp.model.api.service.UserService;
import com.chcit.mobile.mvp.model.entity.BpartnerBean;
import com.chcit.mobile.mvp.model.entity.MonitorCode;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.chcit.mobile.mvp.model.entity.Statu;
import com.google.gson.Gson;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictProvider;

@FragmentScope
public class CommonModel extends BaseModel {

    @Inject
    public CommonModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    public Observable<List<BpartnerBean>> getBparenerBeans() {
        //使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("accessAll","N");
        return Observable.just(mRepositoryManager
                .obtainRetrofitService(UserService.class)
                .queryBpartners().map(jsonObject1 -> jsonObject1.getJSONArray("rows").toJavaList(BpartnerBean.class)
                ))
                .flatMap(new Function<Observable<List<BpartnerBean>>, ObservableSource<List<BpartnerBean>>>() {
                    @Override
                    public ObservableSource<List<BpartnerBean>> apply(Observable<List<BpartnerBean>> bpartnerBeans) throws Exception {
                        return mRepositoryManager.obtainCacheService(CommonCache.class)
                                .getBpartners(bpartnerBeans);

                    }
                });
    }

    public Observable<List<Statu>> refList(int id, boolean update) {
        return Observable.just(mRepositoryManager
                .obtainRetrofitService(CommonService.class)
                .refList(id).map(jsonObject1 -> {
                            if (jsonObject1.getTotal() > 0) {
                                return jsonObject1.getRows();
                            } else {
                                throw new Exception(id + "-没有查询到数据！");
                            }
                        }
                ))
                .flatMap((Function<Observable<List<Statu>>, ObservableSource<List<Statu>>>) status
                        ->
                        mRepositoryManager.obtainCacheService(CommonCache.class)
                                .getStatus(status, new DynamicKey(id), new EvictProvider(update)));
    }


    public Observable<List<MonitorCode>> getMonitorCodes(String method, JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .getMonitorCodes(method, json)
                .map(responseBean -> {
                    if (responseBean.isSuccess()) {
                        return responseBean.getData().getRows();
                    } else {
                        throw new Exception("请求失败: " + responseBean.getMsg());
                    }

                });
    }

    public Observable<ResultBean> doConfirm(String method, JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                .request(method, json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(), ResultBean.class);
                    if (resultBean.isSuccess()) {
                        return resultBean;
                    } else {
                        throw new Exception("请求失败: " + resultBean.getMsg());
                    }

                });
    }
}
